package com.zql.LeetCode;

/**
 * Created By ShuHua on 2021/8/22.
 */
public class LongestPalindrome_5 {
    /**
     * 思路：从某一个字符开始向两边扩散，两边字符相等则继续扩散，不相等则返回当前字符串
     * @param s
     * @return
     */
    public String longestPalindrome(String s) {
        String res ="";
        for(int i=0;i<s.length();i++){
            //以i为中心的回文子串
            String s1 = findString(s,i,i);
            //以i和i+1为中心的回文子串
            String s2 = findString(s,i,i+1);
            res = res.length()>s1.length()? res:s1;
            res = res.length()>s2.length()? res:s2;
        }
        return res;
    }

    public String findString(String s,int left,int right){
        while(left>=0 && right<s.length() && s.charAt(left)==s.charAt(right)){
            left--;
            right++;
        }
        return s.substring(left+1,right);
    }
}
